<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">

        <el-form-item label="机构">
          <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 180px;" placeholder="机构"/>
        </el-form-item>

        <el-form-item label="联系人">
          <el-input v-model="listQuery.contactPerson" clearable class="filter-item" style="width: 180px;" placeholder="联系人"/>
        </el-form-item>

        <el-form-item label="联系电话">
          <el-input v-model="listQuery.contactPhone" clearable class="filter-item" style="width: 180px;" placeholder="联系电话"/>
        </el-form-item>

        <!-- <el-form-item label="状态">
          <el-select v-model="listQuery.status" placeholder="请选择" style="width: 90px;">
            <el-option
              v-for="item in status_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item> -->

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>

        <el-button
          v-hasPermi="['admin:department:create']"
          class="filter-item"
          style="float:right"
          type="primary"
          icon="el-icon-plus"
          @click="startAdd">添加</el-button>
      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row >
      <el-table-column align="center" label="机构ID" prop="id" />

      <el-table-column align="center" label="机构名称" prop="name" />

      <el-table-column align="center" label="停车场数量" prop="parkSize">
        <template slot-scope="{row}">
          <span v-if="!Array.isArray(row.parkNameList)">
            {{ row.parkSize }}
          </span>
          <el-tooltip v-else :content="row.parkNameList.join(',')" class="item" effect="dark" placement="top">
            <span class="blue">{{ row.parkSize }}</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column align="center" label="联系人" prop="contactPerson"/>

      <el-table-column align="center" label="联系电话" prop="contactPhone"/>

      <el-table-column align="center" label="创建时间" prop="createTime"/>

      <!-- <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag >{{ getStatus(scope.row.status) }}</el-tag>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['admin:department:update']"
            type="primary"
            size="small"
            @click="startEditDepartment(scope.row)">编辑</el-button>
          <!-- <el-button
            v-hasPermi="['admin:department:delete']"
            type="primary"
            size="small"
            @click="deleteDepartment(scope.row.name, scope.row.id)">删除</el-button> -->
          <el-button v-if="scope.row.qrcode" size="small" type="primary" @click="qrCode(scope.row)">二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :close-on-click-modal="false" :title="dialogType == 1?'添加机构':'修改机构'" :visible.sync="dialogFormVisible" width="28%">
      <el-form ref="dialogForm" :rules="formRules" :model="dialogForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label=" 机构名称  " prop="name" style="width: 100%;">
              <el-input v-model="dialogForm.name" clearable class="filter-item" style="width: 100%;"  placeholder=""/>
            </el-form-item>
          </el-col>
         <!-- <el-col :span="24">
            <el-form-item label=" 项目标识  " prop="project" style="width: 100%;">
              <el-input v-model="dialogForm.project" clearable class="filter-item" style="width: 100%;"
               placeholder="大小写字母和数字，长度8-32"/>
            </el-form-item>
          </el-col> -->
        </el-row>

        <el-form-item label="  联系人 " prop="contactPerson" style="width: 100%;">
          <el-input v-model="dialogForm.contactPerson" clearable class="filter-item" style="width: 100%;" placeholder=""/>
        </el-form-item>
        <el-form-item label="  联系电话  " prop="contactPhone">
          <el-input v-model="dialogForm.contactPhone" onkeyup="value=value.replace(/[^\d-]/g,'')" style="width: 100%;" clearable class="filter-item"  placeholder=""/>
        </el-form-item>
				<el-form-item label=" 访问域名  " prop="accessDomain" style="width: 100%;">
				  <el-input v-model="dialogForm.accessDomain" clearable class="filter-item" style="width: 100%;"  placeholder=""/>
				</el-form-item>
				<el-form-item label=" 缩写  " prop="abbreviation" style="width: 100%;">
				  <el-input v-model="dialogForm.abbreviation" clearable class="filter-item" style="width: 100%;"  placeholder=""/>
				</el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="confirmEdit('dialogForm')">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :visible.sync="qrcodeDialogVisible" title="二维码" width="20%">
      <div id="qrcode" class="qrcode"/>
      <div class="flex_zhcenter" style="margin-top: 15px;font-size: 26px;">
        停车中缴费二维码
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qrcodeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="qrcodeDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, deleteDepartment, createDepartment, updateDepartment,departmentDetail } from '@/api/department'

import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import QRCode from 'qrcodejs2'

export default {
  name: 'Department',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      status_options: [{
        value: '',
        label: '全部'
      }, {
        value: '1',
        label: '启用'
      }, {
        value: '2',
        label: '停用'
      }],
      listQuery: {
        page: 1,
        pageSize: 20
      },
      downloadLoading: false,
      dialogFormVisible: false,
      dialogForm: {},
      dialogType: 1,
      name: '',
      deleteDepartmentId: 0,
      formRules: {
        name: [{ required: true, message: '请输入机构名称', trigger: 'blur' }],
        contactPerson: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        push: [{ required: true, message: '请选择', trigger: 'blur' }],
        // pushAddress: [{ required: true, message: '输入接口地址', trigger: 'blur' }],
        contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        // project: [{ required: true, message: '大小写字母和数字，长度8-32', trigger: 'blur' }],
				accessDomain: [{ required: true, message: '请输入访问域名', trigger: 'blur' }],
				abbreviation: [{ required: true, message: '请输入缩写', trigger: 'blur' }],
      },
      qrcodeDialogVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    qrCode(row) {
      this.qrcodeDialogVisible = true
      this.$nextTick(function() {
        document.getElementById('qrcode').innerHTML = ''
        new QRCode('qrcode', {
          width: 250,
          height: 250,
          text: row.qrcode,
          colorDark: '#ffffff',
          colorLight: '#000000',
          correctLevel: QRCode.CorrectLevel.H
        })
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        let data = response.data || {}
        this.list = data.list || []
        this.total = data.total || 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // getStatus(status) {
    //   if (status === 1) {
    //     return '启用'
    //   }
    //   return '停用'
    // },
    deleteDepartment(name, departmentId) {
      this.$confirm(`您确定删除机构：${name}`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDepartment({ 'id': departmentId }).then(response => {
          this.$message({
            message: '删除成功!',
            type: 'success'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    startAdd() {
      this.dialogFormVisible = true
      this.dialogType = 1
      this.dialogForm = {}
      this.$nextTick(() => {
        this.$refs.dialogForm.clearValidate()
      })
    },
    confirmEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // if(this.dialogForm.project
          // && this.dialogForm.project.length < 8
          // && this.dialogForm.project.length > 32) return this.$message.error('项目标识可以输入大小写字母和数字，长度8-32')
          if (this.dialogType === 1) {
            createDepartment(this.dialogForm).then(response => {
              this.$message({
                message: '添加成功!',
                type: 'success'
              })
              this.getList()
              this.dialogFormVisible = false
              this.dialogForm = {}
            })
          } else {
            updateDepartment(this.dialogForm).then(response => {
              this.$message({
                message: '修改成功!',
                type: 'success'
              })
              this.getList()
              this.dialogFormVisible = false
              this.dialogForm = {}
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    startEditDepartment(row) {
      departmentDetail(row.id).then(response => {
        this.dialogFormVisible = true
        this.dialogType = 2
        this.dialogForm = response.data || {}
        this.$nextTick(() => {
          this.$refs.dialogForm.clearValidate()
        })
      })

    }

  }
}
</script>
<style scoped>
  .blue{
    color: blue;
  }
  .qrcode{
    margin: 0 auto;
    width: 250px;
  }
</style>
